package com.jilian.powerstation.modul.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jilian.powerstation.R;
import com.jilian.powerstation.common.dto.WindowDto;
import com.jilian.powerstation.listener.CustomItemClickListener;

import java.util.List;

public class WindowsAdapter extends RecyclerView.Adapter<WindowsAdapter.ViewHolder> {

    private List<WindowDto> mDatas;
    private Context mContext;
    private CustomItemClickListener listener;

    public WindowsAdapter(List<WindowDto> mDatas, Context context, CustomItemClickListener listener) {
        this.mDatas = mDatas;
        this.mContext = context;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_window, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;


    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.tvKey.setText(mDatas.get(position).getKey());
        holder.tvValue.setText(mDatas.get(position).getValue()+"");

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvKey;
        private TextView tvValue;




        public ViewHolder(@NonNull View itemView, final CustomItemClickListener listener) {
            super(itemView);
            tvKey = (TextView) itemView.findViewById(R.id.tv_key);
            tvValue = (TextView) itemView.findViewById(R.id.tv_value);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(itemView, getAdapterPosition());
                }
            });
        }
    }
}
